!function () {
    "use strict";
    const self = globalThis;
    self.jcUtils = {
        enableDropEvent: function (dropHandler, node = null) {
            var el = node || document;
            el.addEventListener('dragleave', e => {
                e.preventDefault();
            }
            );
            el.addEventListener('dragover', e => {
                e.preventDefault();
            }
            );
            el.addEventListener('dragenter', e => {
                e.preventDefault();
            }
            );
            el.addEventListener('drop', e => {
                e.preventDefault();
                let files = e.dataTransfer.files;
                dropHandler && dropHandler(files);
            });
        },
        enablePasteEvent: function (pasteHandler, node = null) {

            var el = node || document;
            el.addEventListener('paste', e => {
                e.preventDefault();

                let clipboardData = e.clipboardData;
                let items = clipboardData.items;
                pasteHandler && pasteHandler(items);
            });
        },
        textToArrayBuffer: function (s) {
            var i = s.length;
            var n = 0;
            var ba = new Array()
            for (var j = 0; j < i;) {
                var c = s.codePointAt(j);
                if (c < 128) {
                    ba[n++] = c;
                    j++;
                }
                else if ((c > 127) && (c < 2048)) {
                    ba[n++] = (c >> 6) | 192;
                    ba[n++] = (c & 63) | 128;
                    j++;
                }
                else if ((c > 2047) && (c < 65536)) {
                    ba[n++] = (c >> 12) | 224;
                    ba[n++] = ((c >> 6) & 63) | 128;
                    ba[n++] = (c & 63) | 128;
                    j++;
                }
                else {
                    ba[n++] = (c >> 18) | 240;
                    ba[n++] = ((c >> 12) & 63) | 128;
                    ba[n++] = ((c >> 6) & 63) | 128;
                    ba[n++] = (c & 63) | 128;
                    j += 2;
                }
            }
            return new Uint8Array(ba).buffer;
        },
        downloadFile: function (urlOrData, filename) {
            let url = urlOrData;
            if( typeof url === 'string' ){
                filename = filename || url.substring(url.lastIndexOf('/') + 1);
            }
            

            url = window.URL.createObjectURL(new Blob([url])); //转换为可用URl地址
            let link = document.createElement("a"); //创建a标签
            link.style.display = "none"; //使之不可见
            link.href = url; //赋URL地址
            link.setAttribute("download", filename); //设置下载属性、以及文件名
            document.body.appendChild(link); //将a标签插至页面中
            link.click(); //强制触发a标签事件
            link.remove(); //移除a标签
        }
    };
    fetch('../SercertUtility.wasm').then(response =>
        response.arrayBuffer()
    ).then(bytes => WebAssembly.instantiate(bytes)).then(lib => {
        const GetSecret = lib.instance.exports.GetSecret;
        self.jcUtils.GetSecret = () => {
            let ts = Date.now();
            let secret = GetSecret(ts);
            return [ts, secret];
        }
    }).catch(console.error);
}();